package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.TourismUser;
import com.ruoyi.system.domain.enshrine;
import com.ruoyi.system.mapper.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TourismPact;
import com.ruoyi.system.service.ITourismPactService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 收藏Controller
 *
 * @author gqd
 * @date 2020-12-29
 */
@RestController
@RequestMapping("/system/pact")
public class TourismPactController extends BaseController {
    @Autowired
    private ITourismPactService tourismPactService;

    @Resource
    private TourismAccommodationMapper tourismAccommodationMapper; //民宿
    @Resource
    private TourismPactMapper tourismPactMapper;//收藏
    @Resource
    private TourismCharacteristicMapper tourismCharacteristicMapper; //商品
    @Resource
    private TourismActivitiesMapper tourismActivitiesMapper; //活动
    @Resource
    private TourismFoodMapper tourismFoodMapper; //美食
    @Resource
    private TourismSpotMapper tourismSpotMapper; //景点
    @Autowired
    private TokenService tokenService;
    @Resource
    private TourismUserMapper tourismUserMapper;

    /**
     * 查询收藏列表
     */
    @PreAuthorize("@ss.hasPermi('system:pact:list')")
    @GetMapping("/list")
    public TableDataInfo list(TourismPact tourismPact) {
        startPage();
        List<TourismPact> list = tourismPactService.selectTourismPactList(tourismPact);
        return getDataTable(list);
    }

    /**
     * 导出收藏列表
     */
    @PreAuthorize("@ss.hasPermi('system:pact:export')")
    @Log(title = "收藏", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(TourismPact tourismPact) {
        List<TourismPact> list = tourismPactService.selectTourismPactList(tourismPact);
        ExcelUtil<TourismPact> util = new ExcelUtil<TourismPact>(TourismPact.class);
        return util.exportExcel(list, "pact");
    }

    /**
     * 获取收藏详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:pact:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id) {
        return AjaxResult.success(tourismPactService.selectTourismPactById(id));
    }

    /**
     * 新增收藏
     */
    @PreAuthorize("@ss.hasPermi('system:pact:add')")
    @Log(title = "收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TourismPact tourismPact) {
        return toAjax(tourismPactService.insertTourismPact(tourismPact));
    }

    /**
     * 修改收藏
     */
    @PreAuthorize("@ss.hasPermi('system:pact:edit')")
    @Log(title = "收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TourismPact tourismPact) {
        return toAjax(tourismPactService.updateTourismPact(tourismPact));
    }

    /**
     * 删除收藏
     */
    @PreAuthorize("@ss.hasPermi('system:pact:remove')")
    @Log(title = "收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids) {
        return toAjax(tourismPactService.deleteTourismPactByIds(ids));
    }

    /**
     * 收藏和取消收藏的操作
     *
     *状态值（1景点   2民宿   3美食   4特色商品    5活动）
     * @param
     * @return
     */
    @PostMapping("/operationPact")
    public AjaxResult operationPact(@RequestBody Map map,HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if(loginUser == null){
            return AjaxResult.error(201,"您还未注册或者登录，请先注册或登录");
        }else{
            TourismUser tourismUser = tourismUserMapper.selectByUserId(loginUser.getUserId());
            if(tourismUser == null || loginUser.getUserId() == null){
                return AjaxResult.error(201,"您还未注册或者登录，请先注册或登录");
            }else{
                Integer pactId = Integer.parseInt(map.get("pactId").toString());
                Integer pactType = (Integer) map.get("pactType");
                String isKeep = (String) map.get("isKeep");
                System.out.println(isKeep+"isKeep"+pactType+"pactType"+pactId+"pactId"+loginUser.getUserId());
                if (isKeep.equals("1")) {
                    return tourismPactService.insertPact(loginUser.getUserId(), pactId, pactType);
                }else{
                    return tourismPactService.deletePactId(loginUser.getUserId(),pactId,pactType);
                }
            }
        }
    }

    /**
     * 我的收藏
     * @param map
     * @return
     */
    @PostMapping("/selectPactAll")
    public AjaxResult selectPactAll(@RequestBody Map map,HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        if (loginUser == null) {
            return AjaxResult.error(201, "您还未注册或者登录，请先注册或登录");
        } else {
            Integer userId = loginUser.getUserId();
            TourismUser tourismUser = tourismUserMapper.selectByUserId(loginUser.getUserId());
            if (tourismUser == null || loginUser.getUserId() == null) {
                return AjaxResult.error(201, "您还未注册或者登录，请先注册或登录");
            }else {
                //Integer userId = (Integer) map.get("userId");
                Integer pactType = (Integer) map.get("pactType");
                //AjaxResult ajaxResult = new AjaxResult();
                List<TourismPact> tourismPacts = tourismPactMapper.selectId(userId, pactType);
                    if(tourismPacts.size() == 0 || tourismPacts == null){
                        return AjaxResult.error(tourismPacts);
                    }
                if (pactType == 0) {
                    List<enshrine> enshrines = tourismSpotMapper.selectById(tourismPacts);
                    //ajaxResult.put("enshrines", enshrines);
                    return AjaxResult.success(enshrines);
                } else if (pactType == 1) {
                    List<enshrine> enshrines = tourismAccommodationMapper.selectById(tourismPacts);
                    //ajaxResult.put("enshrines", enshrines);
                    return AjaxResult.success(enshrines);
                } else if (pactType == 2) {
                    List<enshrine> enshrines = tourismFoodMapper.selectById(tourismPacts);
                    //ajaxResult.put("enshrines", enshrines);
                    return AjaxResult.success(enshrines);
                } else if (pactType == 3) {
                    List<enshrine> enshrines = tourismCharacteristicMapper.selectByIds(tourismPacts);
                    //ajaxResult.put("enshrines", enshrines);
                    return AjaxResult.success(enshrines);
                } else {
                    List<enshrine> enshrines = tourismActivitiesMapper.selectById(tourismPacts);
                    //ajaxResult.put("enshrines", enshrines);
                    return AjaxResult.success(enshrines);
                }
            }
        }
    }

}
